<?php
//分区
namespace app\crmindex\model;

use think\cache\driver\Memcache;
use think\Db;

class Subregion extends \ShopModel
{
    protected $table = 'sh_crm_subregion';

    public function subregionDb(){
        return Db::name('crm_subregion');
    }
    //获取社区列表
    public function subregionList($staff_id){

        $staffModel = new Staff();
        $staffInfo = $staffModel->getInfo($staff_id);
        //员工
        $allList = $this->subregionDb()->select();
        if($staffInfo['is_downstream']==1){
            //获取社区列表

            foreach ($allList as $k=>$v){
            $crm_subregion_sys = Db::name('crm_subregion_sys')->where('subregion_id',$v['subregion_id'])->find();

            if(!empty($crm_subregion_sys)){

                    //获取是否有发布权
                    if($crm_subregion_sys['is_staff_issue']==0){

                        $allList[$k]['issue'] = 1;
                    }else{
                        $Iu = Db::name('crm_subregion_issue_staff')->where('staff_id',$staff_id)->where('subregion_id',$v['subregion_id'])->count();
                        if($Iu==1){
                            $allList[$k]['issue'] = 1;
                        }else{
                            $allList[$k]['issue'] = 0;
                        }
                    }
                    //判断是否有查看权
                    if($crm_subregion_sys['is_staff_all_visible']==1){
                        $Ia = Db::name('crm_subregion_visible_staff')->where('staff_id',$staff_id)->where('subregion_id',$v['subregion_id'])->count();
                        if($Ia==0){
                            unset($allList[$k]);
                        }
                    }
                }else{
                    $allList[$k]['issue'] = 1;
                }
            }
            //获取社区下分类列表
            foreach ($allList as $k1=>$v1){
                $allList[$k1]['classify'] = Db::name('crm_subregion_classify')->where('subregion_id',$v1['subregion_id'])->select();

            }
            //return $allList;
        }
        //下游
        else{
            //获取社区列表
            foreach ($allList as $k=>$v){
                $crm_subregion_sys = Db::name('crm_subregion_sys')->where('subregion_id',$v['subregion_id'])->find();
                if(!empty($crm_subregion_sys)){

                    //获取是否有发布权=
                    if($crm_subregion_sys['is_downstream_issue']==0){

                        $allList[$k]['issue'] = 0;
                    }else{
                        $allList[$k]['issue'] = 1;
                    }
                    //判断是否有查看权
                    if($crm_subregion_sys['is_downstream_all_visible']==2){
                        $Ia = Db::name('crm_subregion_visible_downstream')->where('downstream_id',$staff_id)->where('subregion_id',$v['subregion_id'])->count();
                        if($Ia==0){
                            unset($allList[$k]);
                        }
                    }elseif ($crm_subregion_sys['is_downstream_all_visible']==1){
                            if($staffInfo['is_audit']==0){
                                unset($allList[$k]);
                            }
                    }
                }else{
                    $allList[$k]['issue'] = 1;
                }
                //获取社区下分类列表


            }

            foreach ($allList as $k1=>$v1){
                $allList[$k1]['classify'] = Db::name('crm_subregion_classify')->where('subregion_id',$v1['subregion_id'])->select();

            }


            }
        //p(array_values($allList));
        return array_values($allList);

            //获取社区下分类列表

    }




}